package tech.aistar.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 通过底层的servlet代码来学会转发
 */
@WebServlet(urlPatterns = "/test/forward")
public class TestForwardAction extends HttpServlet{
    /**
     * HttpServletRequest - 所有的请求的头信息全部被封装到这个对象中
     * HttpServletResponse - 所有的响应的头信息全部封装到这个对象中
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //打桩 - 产品上线之前，必须将程序中所有的打印全部删除，因为底层调用硬件，性能很低的。
        System.out.println("===servlet===");

        //转发 - webapp根目录下的hello.jsp
        //req.getRequestDispatcher("/hello.jsp").forward(req,resp);

        //重定向
        //resp.sendRedirect("/boot-demo/hello.jsp");

        //WEB-INF - 这是安全级别比较高的目录，必须通过后台进行转发 - 不允许通过地址栏访问

        //req.getRequestDispatcher("/WEB-INF/helloworld.jsp").forward(req,resp);

        //resp.sendRedirect("/boot-demo/WEB-INF/helloworld.jsp");

        //演示request作用域
        //req.setAttribute("username","success");
        //req.getRequestDispatcher("/hello.jsp").forward(req,resp);

        //resp.sendRedirect("/boot-demo/hello.jsp");

        //演示session作用域
        HttpSession session = req.getSession();

        session.setAttribute("username","tom");
        req.getRequestDispatcher("/hello.jsp").forward(req,resp);

        //resp.sendRedirect("/boot-demo/hello.jsp");

    }
}
